package com.chezai.client.dao.manager;

import android.support.annotation.NonNull;

import com.chezai.client.bean.StudentStudyRecord;
import com.chezai.client.dao.DaoManager;
import com.chezai.client.dao.StudentStudyRecordDao;
import com.chezai.client.utils.DateUtil;

import java.util.List;

/**
 * 学员学习记录表管理类
 * Created by likunyang on 2017/9/22.
 */

public class StudentStudyRecordDaoManager {
    private static StudentStudyRecordDaoManager newInstance;
    private StudentStudyRecordDao dao;

    private StudentStudyRecordDaoManager() {
        dao = DaoManager.getInstance().getDaoSession().getStudentStudyRecordDao();
    }

    public static StudentStudyRecordDaoManager getInstance() {
        if (newInstance == null) {
            newInstance = new StudentStudyRecordDaoManager();
        }
        return newInstance;
    }

    /**
     * 通过教练登录记录ID 查找全部相关学员签到记录
     *
     * @param coachLoginRecordId
     * @return 学员登录时间降序
     */
    public List<StudentStudyRecord> findStudentLoginRecordes(long coachLoginRecordId) {
        List<StudentStudyRecord> list = dao.queryBuilder()
                .where(StudentStudyRecordDao.Properties.CoachLoginRecordId.eq(coachLoginRecordId))
                .orderDesc(StudentStudyRecordDao.Properties.UpdateTime)
                .list();
        return list;
    }

    /**
     * 查找全部未签退学员记录
     *
     * @param coachLoginRecordId
     * @return
     */
    public List<StudentStudyRecord> findUnSignoutStudentLoginRecordes(long coachLoginRecordId) {
        List<StudentStudyRecord> list = dao.queryBuilder()
                .where(StudentStudyRecordDao.Properties.CoachLoginRecordId.eq(coachLoginRecordId)
                        , StudentStudyRecordDao.Properties.SiginOutTime.isNull())
                .list();
        return list;
    }

    /**
     * 查找学员登录记录
     *
     * @param coachLoginRecordId 当前登录教练ID
     * @param studentId          学员ID
     * @return
     */
    public StudentStudyRecord findStudentLoginRecord(long coachLoginRecordId, long studentId) {
        StudentStudyRecord studyRecord = dao.queryBuilder()
                .where(StudentStudyRecordDao.Properties.CoachLoginRecordId.eq(coachLoginRecordId)
                        , StudentStudyRecordDao.Properties.StudentId.eq(studentId)
                        , StudentStudyRecordDao.Properties.SiginOutTime.isNull())
                .unique();
        return studyRecord;
    }


    public StudentStudyRecord findStudentLoginRecordByClassRecordId(long classRecordId) {
        StudentStudyRecord studyRecord = dao.queryBuilder()
                .where(StudentStudyRecordDao.Properties.ClassRecordId.eq(classRecordId))
                .unique();
        return studyRecord;
    }

    public StudentStudyRecord findStudentLoginRecord(long studentLoginRecordId) {
        StudentStudyRecord studentStudyRecord = dao.load(studentLoginRecordId);
        return studentStudyRecord;
    }

    /**
     * 插入学员登录记录
     *
     * @param coachLoginRecordId 教练登录记录ID
     * @param studenInfoId       学员信息ID
     * @return
     */
    public long insertStudentStudyRecord(@NonNull long coachLoginRecordId, long studenInfoId, long classRecordId) {
        StudentStudyRecord studyRecord = new StudentStudyRecord();
        studyRecord.setCoachLoginRecordId(coachLoginRecordId);
        studyRecord.setStudentId(studenInfoId);
        studyRecord.setClassRecordId(classRecordId);
        studyRecord.setCreateDay(DateUtil.timestampStr("yyyy/MM/dd"));
        studyRecord.setSiginInTime(System.currentTimeMillis());
        studyRecord.setUpdateTime(System.currentTimeMillis());
        long studentLoginRecordId = dao.insert(studyRecord);
        return studentLoginRecordId;
    }

    /**
     * 更新学员登出时间
     *
     * @param studentLoginRecordId
     */
    public StudentStudyRecord updateStudentSignOutTime(long studentLoginRecordId) {
        StudentStudyRecord studyRecord = findStudentLoginRecord(studentLoginRecordId);
        studyRecord.setSiginOutTime(System.currentTimeMillis());
        studyRecord.setUpdateTime(System.currentTimeMillis());
        dao.update(studyRecord);
        return studyRecord;
    }

}
